package v1

import (
	"github.com/gin-gonic/gin"
	"ota/bll"
	"ota/model"
	"ota/server/web/middleware"
	"ota/utils"
)

var UpgradePlan = &upgradePlan{}

func init() {
	RegisterRouter(UpgradePlan)
}

type upgradePlan struct{}

// Init
func (a *upgradePlan) Init(r *gin.RouterGroup) {
	g := r.Group("/upgradePlan", middleware.Auth())
	{
		g.POST("/create", a.create)
		g.POST("/update", a.update)
		g.POST("/list", a.list)
		g.POST("/listUpgradeObject", a.listUpgradeObject)
		g.POST("/delete", a.delete)
		g.POST("/terminal", a.terminal)
		g.POST("/detail", a.find)
	}
}

// create
func (a *upgradePlan) create(c *gin.Context) {
	var (
		in  = &model.UpgradePlanCreateRequest{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		_ = c.Error(err)
		return
	}

	if err := in.Check(); err != nil {
		_ = c.Error(err)
		return
	}

	if err = bll.UpgradePlan.Create(c.Request.Context(), in); err != nil {
		_ = c.Error(err)
		return
	}
	utils.ResponseOk(c, nil)
}

// update
func (a *upgradePlan) update(c *gin.Context) {
	var (
		in  = &model.UpgradePlanUpdateRequest{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		_ = c.Error(err)
		return
	}

	if err := in.Check(); err != nil {
		_ = c.Error(err)
		return
	}

	if err = bll.UpgradePlan.Update(c.Request.Context(), in); err != nil {
		_ = c.Error(err)
		return
	}
	utils.ResponseOk(c, nil)
}

// terminal
func (a *upgradePlan) terminal(c *gin.Context) {
	var (
		in  = &model.UpgradePlanTerminalRequest{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		_ = c.Error(err)
		return
	}

	if err = bll.UpgradePlan.Terminal(c.Request.Context(), in); err != nil {
		_ = c.Error(err)
		return
	}
	utils.ResponseOk(c, nil)
}

// list
func (a *upgradePlan) list(c *gin.Context) {
	var (
		in  = &model.UpgradePlanListRequest{}
		out = &model.UpgradePlanListResponse{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		_ = c.Error(err)
		return
	}

	if out, err = bll.UpgradePlan.List(c.Request.Context(), in); err != nil {
		_ = c.Error(err)
		return
	}
	utils.ResponseOk(c, out)
}

// listUpgradeObject
func (a *upgradePlan) listUpgradeObject(c *gin.Context) {
	var (
		in  = &model.ListUpgradeObjectRequest{}
		out = &model.UserUpgradeListResponse{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		_ = c.Error(err)
		return
	}

	if out, err = bll.UpgradePlan.ListUpgradeObject(c.Request.Context(), in); err != nil {
		_ = c.Error(err)
		return
	}
	utils.ResponseOk(c, out)
}

// list
func (a *upgradePlan) find(c *gin.Context) {
	var (
		in  = &model.UpgradePlanInfoRequest{}
		out = &model.UpgradePlanInfo{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	if out, err = bll.UpgradePlan.Find(c.Request.Context(), in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOk(c, out)
}

// delete
func (a *upgradePlan) delete(c *gin.Context) {
	var (
		in  = &model.UpgradePlanDeleteRequest{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	if err = bll.UpgradePlan.Delete(c.Request.Context(), in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOk(c, nil)
}
